package com.ojoin.ojoin.eb_spider.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ojoin.ojoin.eb_spider.check.Dispatcher;
import com.ojoin.ojoin.eb_spider.domain.AlipayAccount;
import com.ojoin.ojoin.eb_spider.domain.AlipayCheckException;
import com.ojoin.ojoin.eb_spider.service.AlipayAccountService;
import com.ojoin.ojoin.eb_spider.service.AlipayCheckExceptionService;
import com.ojoin.ojoin.eb_spider.service.CaiNaioInventoryService;
import com.ojoin.ojoin.eb_spider.service.StatTotalService;
import com.ojoin.ojoin.eb_spider.utils.WebServiceUtils;
import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.NumberUtils;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanStatTotal;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformInfo;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyInventory;

@Service
public class AlipayAccountServiceImpl implements AlipayAccountService {
	private Logger log = LoggerFactory
			.getLogger(AlipayAccountServiceImpl.class);
	@Autowired 
	private AlipayCheckExceptionService alipayCheckExceptionService;

	@Autowired
	private CaiNaioInventoryService caiNaioInventoryService;

	@Autowired
	private StatTotalService statTotalService;
	
	@Override
	public List<AlipayAccount> getAlipayAccounts(TbSlPlatformUser user)
			throws Exception {
		
		List<TbSlPlatformUser> users = WebServiceUtils.getPlatformUserList(user.getIdOfTbMerchantUser());
		
		List<AlipayAccount> re = new ArrayList<AlipayAccount>();

		AlipayAccount a0 = new AlipayAccount(users,
				"m13366388040@163.com", "m13366388040@163.com",
				"meidiHTWJ2016", AlipayAccount.MAIN,
				TbSlPlatformInfo.ALIPAY, "美的恒通万佳专卖店", true
				);
		AlipayAccount a1 = new AlipayAccount(users, "htwjzyd@163.com",
				"圣华易达电器专营店", "meidiSHYD2016", AlipayAccount.COMMON,
				TbSlPlatformInfo.THIRDSELLER_TMALL, "圣华易达电器专营店", false);
		
		AlipayAccount a2 = new AlipayAccount(users,
				"wanjiaxyin@163.com", "wanjiaxyin@163.com", "xiangyinHTWJ2016",
				AlipayAccount.COMMON, TbSlPlatformInfo.ALIPAY, "象印恒通万佳专卖店",
				true);
		
		AlipayAccount a3 = new AlipayAccount(users,
				"bjhtwj2016@163.com", "bjhtwj2016@163.com", "ht123450",
				AlipayAccount.COMMON, TbSlPlatformInfo.ALIPAY, "美的分销", true);
 

		AlipayCheckException standa0 = Dispatcher.stands.get(a0
				.getUsername());
		AlipayCheckException standa1 = Dispatcher.stands.get(a1
				.getUsername());
		AlipayCheckException standa2 = Dispatcher.stands.get(a2
				.getUsername());
		AlipayCheckException standa3 = Dispatcher.stands.get(a3
				.getUsername());

		if (CommonUtils.isEmpty(standa0)) {
			standa0 = alipayCheckExceptionService
					.getStandard("m13366388040@163.com");
			Dispatcher.stands.put(a0.getUsername(), standa0);
		}

		if (CommonUtils.isEmpty(standa1)) {
			standa1 = alipayCheckExceptionService
					.getStandard("m13366388040@163.com");
			Dispatcher.stands.put(a1.getUsername(), standa1);
		}
		if (CommonUtils.isEmpty(standa2)) {
			standa2 = alipayCheckExceptionService
					.getStandard("m13366388040@163.com");
			Dispatcher.stands.put(a2.getUsername(), standa2);
		}
		
		if (CommonUtils.isEmpty(standa3)) {
			standa3 = alipayCheckExceptionService
					.getStandard("m13366388040@163.com");
			Dispatcher.stands.put(a3.getUsername(), standa3);
		}
		
		
		
		try {
			
			
			
			long money = WebServiceUtils.getTotalNotPayback(user.getIdOfTbMerchantUser());
			a0.getAlipayCheckException().setAmountInArrear(money);
			
			if(!CommonUtils.isEmpty(users)){
				Iterator<TbSlPlatformUser> it = users.iterator();
				while(it.hasNext()){
					TbSlPlatformUser u = it.next();
					if(u.getPlatformName().equals(TbSlPlatformInfo.SUPPLIER_CAINIAO) || u.getPlatformName().equals(TbSlPlatformInfo.THIRDSELLER_CAINIAO)){
						
						Map<String, TbSlSupplyInventory> map = WebServiceUtils.getSupplyInventoryByplateformId(u.getId(), null, null, null);
						
						Collection<TbSlSupplyInventory> li = map.values();
						
						if(!CommonUtils.isEmpty(li)){
							float total = 0 ;
							String detail = "";
							for(TbSlSupplyInventory tbsl:li){
								total += tbsl.getTotalCost();
								detail += tbsl.getItemId()+"</br>";
							}
							
							 a0.getAlipayCheckException().setCaiNiaoSettleorder(Float.valueOf(total));
							 a0.getAlipayCheckException().setCaiNiaoSettleorderDetail(detail);
							 
						}
						/*TbCatchdataStrategy ca = WebServiceUtils
								.getCatchdataStrategyByUserId(u.getId());
						
						TbSlLoanStatTotal total = statTotalService.stat(u, ca);*/
						
			           
					}else if(u.getPlatformName().equals(TbSlPlatformInfo.SUPPLIER_TMALL) || u.getPlatformName().equals(TbSlPlatformInfo.THIRDSELLER_TMALL)){
						 
						TbSlLoanStatTotal total = statTotalService.stat(u);
						
						a0.getAlipayCheckException().setTmallUnpaySettleorder(Float.valueOf(total.getTotalAll()*NumberUtils.getLongAmount(u.getDiscount())/10000));
						a0.getAlipayCheckException().setTmallUnpaySettleorderDetail("金额:["+total.getTotalAll()+"]*比例:["+NumberUtils.getLongAmount(u.getDiscount())/10000+"]");
					}
					
					
					
				}
			}
			
			
		} catch (ClientProtocolException e) {
			LogUtils.error(log, e);
			a0.getAlipayCheckException().getException().put("获取贷款金额", e);
		} catch (Exception e) {
			a0.getAlipayCheckException().getException().put("获取贷款金额", e);
			LogUtils.error(log, e);
		}

		
		
		a0.setStandard(standa0);
		a1.setStandard(standa1);
		a2.setStandard(standa2);
		a3.setStandard(standa3);

		re.add(a0);
		// re.add(a1);
		re.add(a2);
		re.add(a3);

		return re;
	}

	@Override
	public boolean needSend(List<AlipayAccount> lists) {
		boolean needSend = false;

		if (!CommonUtils.isEmpty(lists)) {
			Iterator<AlipayAccount> it = lists.iterator();
			while (it.hasNext()) {
				AlipayAccount aa = it.next();
				AlipayCheckException ac = aa.getAlipayCheckException();
				if (alipayCheckExceptionService.needToSend(aa.isNeedCheck(),
						ac, aa.getStandard())) {
					needSend = true;
					break;
				}
			}
		}

		return needSend;
	}

	@Override
	public boolean initSendMailStyle(List<AlipayAccount> lists) {
		/*
		 * if (!CommonUtils.isEmpty(lists)) { Iterator<AlipayAccount> it =
		 * lists.iterator(); while (it.hasNext()) { AlipayAccount aa =
		 * it.next(); AlipayCheckException ac = aa.getAlipayCheckException();
		 * alipayCheckExceptionService.initSendMailStyle(aa.isNeedCheck(),
		 * ac,aa.getStandard()); } }
		 */

		return true;
	}

	/**
	 * 获取可销总金额
	 * 
	 * @return
	 */
	public Float getCanSell(AlipayAccount alipayAccount) {

		if (CommonUtils.isEmpty(alipayAccount)) {
			throw new BusinessException("主账号为空");
		}

		if (CommonUtils.isEmpty(alipayAccount.getAlipayCheckException().getCaiNiaoSettleorder())) {
			throw new BusinessException("菜鸟统计金额为空");
		}

		if (CommonUtils.isEmpty(alipayAccount.getAlipayCheckException().getAvailableBalance())) {
			throw new BusinessException("主账号抓取支护宝可用余额为空");
		}

		if (CommonUtils.isEmpty(alipayAccount.getAlipayCheckException().getAmountInArrear())) {
			throw new BusinessException("欠款金额为空");
		}

		if (CommonUtils.isEmpty(alipayAccount.getAlipayCheckException().getTmallUnpaySettleorder())) {
			throw new BusinessException("天猫统计金额为空");
		}
		
		return alipayAccount.getAlipayCheckException().getCaiNiaoSettleorder()
				- alipayAccount.getAlipayCheckException().getAmountInArrear()
				+ alipayAccount.getAlipayCheckException().getAvailableBalance()+(float)(alipayAccount.getAlipayCheckException().getTmallUnpaySettleorder());
	}

}
